﻿using CPPEI.ETL.DataServiceInterface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.DSImplement
{
    public class DSDtSerProxy : IDataServiceProxy, IDetectable, IFeedback
    {
        public DataServiceInterface.TDataService GetSchema(DataServiceInterface.DataServiceInfo info)
        {
            DataServiceInterface.TDataService dataService = new TDataService();
            dataService.Name = info.Address;
            dataService.Address = info.Address;
            return dataService;
        }

        /// <summary>
        /// 获取数据源的结构，这里只能获取到参数信息，列的信息只能通过获取data之后来解析
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public DataServiceInterface.TDataSource GetSchema(DataServiceInterface.DataSourceInfo info)
        {
            Guid serviceId = new Guid(info.DataSourceValue);
            DataServiceRef.DataService dataService = new DataServiceRef.DataService();
            dataService.Url = info.Address;

            DataServiceRef.DataParameter[] parameters = dataService.GetParameters(serviceId);

            DataServiceInterface.TDataSource tSource = new TDataSource();
            tSource.Name = info.DataSource;
            tSource.Value = info.DataSourceValue;
            tSource.Type = "Source";

            for (int index = 0; index < parameters.Length; index++)
            {
                TDataSourceParameter tParameter = new TDataSourceParameter(parameters[index].Name, CPPEI.ETL.Infrastructure.TypeUtil.ConvertFromCSType(parameters[index].Type));

                tSource.Parameters.Add(tParameter);
            }
            return tSource;
        }

        /// <summary>
        /// info.dataSource=DataSourceValue
        /// </summary>
        /// <param name="info"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public DataServiceInterface.IData GetData(DataServiceInterface.DataSourceInfo info, Dictionary<string, object> args)
        {
            DataServiceRef.DataService dataService = new DataServiceRef.DataService();
            dataService.Url = info.Address;

            Guid appId = new Guid(info.AuthUser);
            string token = info.AuthPassword;

            List<DataServiceRef.DataParameter> parameters = new List<DataServiceRef.DataParameter>();
            foreach (string key in args.Keys)
            {
                DataServiceRef.DataParameter parameter = new DataServiceRef.DataParameter();
                parameter.Name = key;
                parameter.Value = args[key].ToString();

                parameters.Add(parameter);
            }

            Guid dataSetId = new Guid(info.DataSourceValue);

            DataTable dataTable = dataService.GetData(appId, token, dataSetId, parameters.ToArray());

            return new DataServiceInterface.DataTableData(dataTable);
        }

        public List<string> Enumerate(DataServiceInterface.DataServiceInfo info)
        {
            return null;
        }

        public List<TDataSource> EnumerateSources(DataServiceInfo service)
        {
            DataServiceRef.DataService dataService = new DataServiceRef.DataService();
            dataService.Url = service.Address;

            DataServiceRef.DataSourceSummary[] dataSetArr = dataService.GetDataSources();

            List<TDataSource> dataSourceList = new List<TDataSource>();
            for (int index = 0; index < dataSetArr.Length; index++)
            {
                TDataSource sourceInfo = new TDataSource();
                sourceInfo.Name = dataSetArr[index].Name;
                sourceInfo.Type = "Source";// DEF.DSDtSourceType.Source;
                sourceInfo.Value = dataSetArr[index].Id.ToString();
                sourceInfo.Description = dataSetArr[index].Description;

                dataSourceList.Add(sourceInfo);
            }

            return dataSourceList;
        }

        public List<TDataSourceParameter> EnumeratePrameters(DataSourceInfo source)
        {
            DataServiceRef.DataService dataService = new DataServiceRef.DataService();
            dataService.Url = source.Address;

            Guid dataSourceId = new Guid(source.DataSourceValue);
            DataServiceRef.DataParameter[] parameterArr = dataService.GetParameters(dataSourceId);


            List<TDataSourceParameter> parameters = new List<TDataSourceParameter>();
            for (int index = 0; index < parameterArr.Length; index++)
            {
                TDataSourceParameter parameter = new TDataSourceParameter(parameterArr[index].Name, 
                    CPPEI.ETL.Infrastructure.TypeUtil.ConvertFromCSType(parameterArr[index].Type),
                    parameterArr[index].Description);

                parameters.Add(parameter);
            }
            return parameters;
        }

        public List<TDataSourceColumn> EnumerateColumns(DataSourceInfo source)
        {
            throw new NotImplementedException();
        }

        #region implement IFeedback
        public void Feedback(DataSourceInfo source, Guid batchId, Guid sourceId, long dataCount, Guid[] passedData, Guid[] unpassedData)
        {
            DataServiceRef.DataService dataService = new DataServiceRef.DataService();
            dataService.Url = source.Address;
            

            Guid appId = new Guid(source.AuthUser);
            string token = source.AuthPassword;
            Guid dataSourceId = new Guid(source.DataSourceValue);

            dataService.Feedback(appId, token, dataSourceId, batchId, (int)dataCount, passedData, unpassedData);
        }
        #endregion
    }
}
